package com.http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.os.AsyncTask;

import com.android.ocean.util.net.ErrorCode;
import com.android.ocean.util.net.NetConUtil;
import com.android.ocean.util.net.ResEntity;
import com.common.method.CommonData;
import com.common.method.ServerPath;
import com.modle.UserOrgInfo;
import com.util.JsonUtil;

/**
 * 
 * @ClassName:UserOrgInfoModifyTask
 * @Description:TODO修改机构信息
 * @author zhuw
 * @date 2013-8-23 上午9:53:51
 */
public class UserOrgInfoModifyTask extends
		AsyncTask<Object, Void, ResultUserOrgInfo> {
	public static interface CallBack extends NetWorkCallBack {
		/**
		 * 
		 * @Description:TODO修改用户信息回调
		 * @param@param data
		 * @return void
		 * @author zhuw
		 * @date 2013-8-20 上午10:18:09
		 */
		public abstract void procUserOrgInfoModify(UserOrgInfo data);

	}

	CallBack callback;

	public UserOrgInfoModifyTask(CallBack callback) {
		super();
		this.callback = callback;
	}

	@Override
	protected ResultUserOrgInfo doInBackground(Object... params) {
		ResultUserOrgInfo data = null;
		try {
			HashMap<String, Object> map = (HashMap<String, Object>) params[0];
			ArrayList<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
			if (map != null && !map.isEmpty()) {
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					paramPairs.add(new BasicNameValuePair(entry.getKey(), entry
							.getValue().toString()));

				}
			}
			ResEntity re = NetConUtil.postByForm(
					ServerPath.ORG_INFO_MODIFY_PATH, paramPairs);
			if (re.code == ResEntity.S_OK) {
				data = new ResultUserOrgInfo(CommonData.H_FLAG_SUCCESS, null,
						JsonUtil.parseUserOrgInfo(new JSONObject(re.jsonStr)));

			} else if (re.code == ResEntity.S_FAILED) {
				data = new ResultUserOrgInfo(CommonData.H_FLAG_ERROR, "code:"
						+ re.code + "    " + re.jsonStr, null);

			} else {
				data = new ResultUserOrgInfo(CommonData.H_FLAG_ERROR, "code:"
						+ re.code + ErrorCode.ERROR_MSG, null);
			}
		} catch (Exception e) {
			data = new ResultUserOrgInfo(CommonData.H_FLAG_ERROR,
					ErrorCode.ERROR_MSG, null);
			e.printStackTrace();

		}
		return data;
	}

	@Override
	protected void onPostExecute(ResultUserOrgInfo result) {
		if (result.flag == CommonData.H_FLAG_SUCCESS) {
			callback.procUserOrgInfoModify(result.data);

		} else {
			callback.procError(result.msg);
		}

	}
}
